#include "mod_ftp_update.h"
#include "data.h"
#include "mod_ftp.h"
#include "sal_module_wrapper.h"
#include <Net_Config.h>

#define UPDATE_FILE_NAME "MasterAPP.bin"

static int8_t g_ftp_status = 0;

extern int32_t ftpc_info_init(char_t *user, char_t *pwd, char_t *path, char_t *fname);

static void ftpc_notify (uint8_t event) 
{
	switch (event) 
	{
		/* 文件操作成功 */
		case FTPC_EVT_SUCCESS:
            g_ftp_status = FTP_STATUS_SUCCESS;
			printf("Command successful\n");
			break;

		/* 失败，因为FTP服务器响应时间溢出，因此FTP客户端终止操作 */
		case FTPC_EVT_TIMEOUT:
            g_ftp_status = FTP_STATUS_FAIL;
			printf("Failed, timeout expired\n");
			break;

		/* 失败，FTP客户端登陆FTP服务器失败 */
		case FTPC_EVT_LOGINFAIL:
            g_ftp_status = FTP_STATUS_FAIL;
			printf("Failed, username/password invalid\n");
			break;

		/* 失败，禁止操作此文件 */
		case FTPC_EVT_NOACCESS:
            g_ftp_status = FTP_STATUS_FAIL;
			printf("Failed, operation not allowed\n");
			break;

		/* 失败，在FTP服务器上找不到请求的文件 */
		case FTPC_EVT_NOTFOUND:
            g_ftp_status = FTP_STATUS_FAIL;
			printf("Failed, file or path not found\n");
			break;

		/* 失败，在FTP服务器上找不大工作目录路径 */
		case FTPC_EVT_NOPATH:
            g_ftp_status = FTP_STATUS_FAIL;
			printf("Failed, working directory not found\n");
			break;

		/* 失败，文件打开或者写入出错，即FTP客户端操作SD卡或者其它存储介质出错 */
		case FTPC_EVT_ERRLOCAL:
            g_ftp_status = FTP_STATUS_FAIL;
			printf("Failed, local file open/write error\n");
			break;

		/* 失败，未指定的协议错误，或者说在文件操作过程中遇到错误 */
		case FTPC_EVT_ERROR:
            g_ftp_status = FTP_STATUS_FAIL;
			printf("Failed, unspecified protocol error\n");
			break;
	}
}
#if 0
static void w4g_ftp_notify (uint8_t event){
    switch (event) 
	{
		/* 文件操作成功 */
        case FTP_RET_SUCCESS:
            g_ftp_status = FTP_STATUS_SUCCESS;
			printf("w4g ftp successful\n");            
        break;
        case FTP_RET_ERR:
            g_ftp_status = FTP_STATUS_FAIL;
            printf("w4g ftp fail\n"); 
        break;
        default:
            g_ftp_status = FTP_STATUS_FAIL;
            printf("w4g ftp, unspecified protocol error\n");
        break;
    }
}
#endif
static int32_t ftp_line_get_file(char_t *ip, int32_t port, char_t *user, char_t *pwd, char_t *path, char_t *fname){
    g_ftp_status = FTP_STATUS_SUCCESS;
    
    ftpc_info_init(user,pwd,path,fname);

    uint8_t addr[4];
    int32_t addr_val[4]={0};
    sscanf(ip, "%d.%d.%d.%d", &addr_val[0], &addr_val[1], &addr_val[2], &addr_val[3]);

	  addr[0] = addr_val[0];
    addr[1] = addr_val[1];
    addr[2] = addr_val[2];
    addr[3] = addr_val[3];
        
    if (ftpc_connect ((U8 *)addr, port, FTPC_CMD_GET, ftpc_notify) == 0) 
    {
        printf("Connect failed, the client is busy.\n");
        g_ftp_status = FTP_STATUS_FAIL;
        return -1;
    }
    g_ftp_status = FTP_STATUS_LOADING;
    printf("FTP client started.\n");
    return 0;
}

static int32_t ftp_w4g_get_file(char_t *ip, int32_t port, char_t *user, char_t *pwd, char_t *path, char_t *fname){

    g_ftp_status = FTP_STATUS_SUCCESS;
    if (ftp_login(ip,port,user,pwd) != 0){
        return -1;
    }
    char_t file[50]={0};
    sprintf(file, "%s\%s",path, fname);
    int len =  ftp_filesize(file);
    if (len <= 0){
        ftp_quit();
        return -1;
    }
		//uint8_t md5_cal[] = {0x78,0xd0,0xa7,0xb2,0xe0,0xef,0xf8,0xc6,0x13,0x0b,0x66,0xa6,0x08,0xc8,0xb7,0xf7};
		uint8_t md5_cal[] = {0x6d,0xe6,0x5f,0xd7,0xaf,0x15,0xde,0x38,0x2a,0xc0,0xd6,0x61,0xcc,0x08,0x97,0x82};
    if (ftp_download(file, len,md5_cal) != 0){
        ftp_quit();
        return -1;
    }
    
    ftp_quit();

    return 0;
}

int32_t ftp_get_file(char_t *ip, int32_t port, char_t *user, char_t *pwd, char_t *path, char_t *fname){
    char_t netmode[20]={0};
    data_getstr("netmode", netmode, sizeof(netmode));
    printf("net mode:%s\r\n", netmode);
    if (strncmp(netmode, "gprs", 4) == 0){
        return -1;
    }else if(strncmp(netmode, "line", 4) == 0){
        return ftp_line_get_file(ip, port, user, pwd, path, fname);
    }else if (strncmp(netmode, "gm510", 4) == 0) {
        return ftp_w4g_get_file(ip, port, user, pwd, path, fname);
    }
	return -1;
}

int32_t ftp_get_status(void){
    return g_ftp_status;
}


